﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FBroSharp.DataType;
using FBroSharp;
using BaseTest;
using System.Windows.Forms.VisualStyles;
using FBroSharp.Lib;

namespace _008008动作类
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CreateBrowser();
        }
        public void CreateBrowser()
        {
            string url = "https://www.baidu.com";
            FBroSharpWindowsInfo windowsInfo = new FBroSharpWindowsInfo
            {
                parent_window = label_浏览器.Handle,
                x = 0,
                y = 0,
                width = label_浏览器.Width,
                height = label_浏览器.Height
            };
            //自定义浏览器事件
            BrowserEvent browser_event = new BrowserEvent(this);
            //创建浏览器
            FBroSharpControl.CreatBrowser(url, windowsInfo, default, default, default, browser_event, default);

        }
        //窗口大小被改变时，更新浏览器的大小
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < BrowserList.data.Count; i++)
            {
                BrowserList.data[i].MoveWindow(0 , 0 , label_浏览器.Width, label_浏览器.Height, true);
            }
        }
        public void UpdataLoadingState(bool isLoading, bool canGoBack, bool canGoForward)
        {
            if (this.label1.InvokeRequired)
            { 
                this.label1.Invoke(new Action(() =>
                {
                    //UpdataLoadingState(isLoading, canGoBack, canGoForward);
                    //给label1标签赋值
                    label1.Text = isLoading ? "正在加载..." : "加载完成";
                    //给按钮赋值
                    button_后退.Enabled = canGoBack;
                    button_前进.Enabled = canGoForward;
                }));
           
            }
            
        }

        public void UpdataStatusMessage(string message)
        {
            if (this.label2.InvokeRequired)
            { 
                this.label2.Invoke(new Action(() =>
                {
                    //给label2标签赋值
                    label2.Text = message;
                }));
            }
        }

        private void ButtonClick(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            FBroSharpBrowser browser = BrowserList.data[0];
            if (btn == button_后退)
            {
                browser.GoBack();
            }
            else if (btn == button_前进)
            {
                browser.GoForward();
            }
            else if (btn == button_重新加载)
            {
                browser.Reload();

            }
            else if (btn == button_忽略缓存重新加载)
            {
                browser.ReloadIgnoreCache();
            }
            else if (btn == button_停止加载)
            {
                browser.StopLoad();
            }
        }
    }
}
